import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.util.HashSet;
import java.util.Set;

/*
 * @Author: YiMing Xu
 * @Date: 2019/1/9
 * @Description: 生成测试数据，随机产生[0，SIZE)内不重复的数字
 * @Version 1.0
 **/
public class TestDataGenerator {
    private Integer size;
    private String fileName;

    /*
     * @Author: YiMing Xu
     * @Date: 2019/1/10
     * @Description: 构造函数，生成存放目录
     * @Param: [size, fileName]
     * @return:
     **/
    public TestDataGenerator(Integer size, String fileName) {
        this.size = size;
        this.fileName = fileName;
        String dir[] = fileName.split("/"); //获取所有目录
        StringBuffer stringBuffer = new StringBuffer();
        stringBuffer.append(dir[0]);
        for (int i = 1; i < dir.length - 1; i++) {
            stringBuffer.append("/");
            stringBuffer.append(dir[i]);
        }
        File f = new File(stringBuffer.toString());
        if (!f.exists()) {  //如果目录不存在，则创建目录
            f.mkdirs();
        }
    }

    /*
     * @Author: YiMing Xu
     * @Date: 2019/1/9
     * @Description: 产生测试数据，数据量为size
     * @Param: []
     * @return: void
     **/
    public void generator() throws Exception {
        BufferedWriter out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fileName + ".txt")));
        int temp[] = new int[size];
        for (int i = 0; i < size; i++) {
            temp[i] = 0;
        }
        int id;
        String name = "TestStu_";
        int score;
        int count = 0;
        while (count != size) {
            id = (int) (Math.random() * size);
            score = (int) (1 + Math.random() * 100);
            StringBuffer stringBuffer = new StringBuffer();
            stringBuffer.append(name);
            if (temp[id] == 0) {
                stringBuffer.append(id);
                out.write(id + " " + stringBuffer.toString() + " " + score + "\n");
                temp[id] = 1;
                count++;
            }
        }
        System.out.println("Successfully generate " + count + " records.");
        out.close();
    }
}
